package com.itheima.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

/**
 * 短信发送工具类
 */
public class SMSUtils {
	public static final String VALIDATE_CODE = "SMS_195872345";//发送短信 验证码
	public static final String ORDER_NOTICE = "SMS_195862042";//体检预约成功 短信通知

	/**
	 * 发送短信验证码
	 * @param phoneNumbers
	 * @param param
	 * @throws ClientException
	 */
	//这代码专门是用来发送 短信验证码的
	public static void VALIDATE_CODE(String phoneNumbers,String param) throws ClientException{
		DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4G3PbcJbz4zdBm1SbxaF", "cZS8gcq7JZQYMoLvbusHTRG4xCuFNd");
		IAcsClient client = new DefaultAcsClient(profile);
		CommonRequest request = new CommonRequest();
		request.setMethod(MethodType.POST);
		request.setDomain("dysmsapi.aliyuncs.com");
		request.setVersion("2017-05-25");
		request.setAction("SendSms");
		request.putQueryParameter("RegionId", "cn-hangzhou");
		// phoneNumbers  是收到短息的人的 手机号码  是用户传入进来的参数
		request.putQueryParameter("PhoneNumbers", phoneNumbers);
		// 这里的 value 是模板管理中  的 模板名称；
		request.putQueryParameter("SignName", "验证码");
		// 这 TemplateCode 就是 发送验证码 或者电信的 名称 代表需要发送的短信的类型
		request.putQueryParameter("TemplateCode", VALIDATE_CODE);
		request.putQueryParameter("TemplateParam", "{\"code\":\""+param+"\"}");
		try {
			CommonResponse response = client.getCommonResponse(request);
			System.out.println(response.getData());
		} catch (ServerException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		}
	}



	/**
	 * 短信通知成功
	 * @param phoneNumbers
	 * @param param
	 * @throws ClientException
	 */
	//这代码专门是用来发送 短信验证码的
	public static void ORDER_NOTICE(String phoneNumbers,String param) throws ClientException{
		DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4G3PbcJbz4zdBm1SbxaF", "cZS8gcq7JZQYMoLvbusHTRG4xCuFNd");
		IAcsClient client = new DefaultAcsClient(profile);
		CommonRequest request = new CommonRequest();
		request.setMethod(MethodType.POST);
		request.setDomain("dysmsapi.aliyuncs.com");
		request.setVersion("2017-05-25");
		request.setAction("SendSms");
		request.putQueryParameter("RegionId", "cn-hangzhou");
		// phoneNumbers  是收到短息的人的 手机号码  是用户传入进来的参数
		request.putQueryParameter("PhoneNumbers", phoneNumbers);
		// 这里的 value 是模板管理中  的 模板名称；
		request.putQueryParameter("SignName", "短信通知成功");
		// 这 TemplateCode 就是 发送验证码 或者电信的 名称 代表需要发送的短信的类型
		request.putQueryParameter("TemplateCode", ORDER_NOTICE);
		request.putQueryParameter("TemplateParam", "{\"code\":\""+param+"\"}");
		try {
			CommonResponse response = client.getCommonResponse(request);
			System.out.println(response.getData());
		} catch (ServerException e) {
			e.printStackTrace();
		} catch (ClientException e) {
			e.printStackTrace();
		}
	}



	//1234
	public static void main(String[] args) throws ClientException {
		SMSUtils.ORDER_NOTICE("18073642957","2345");
		SMSUtils.VALIDATE_CODE("18073642957","2019-09-09");
	}
}
